/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerCart.java
 *
 * Created on 2010-3-9, 22:15:38
 */

package eLUX;

import com.elux.ado.order.OrderItem;
import com.elux.system.webcustsys.IWebCustSys;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author binbin
 */
public class CustomerCart extends javax.swing.JDialog {

    private Vector<OrderItem> orderList = new Vector<OrderItem>();
    private IWebCustSys cusSys;
    private int customerID =0;

    /** Creates new form CustomerCart */
    public CustomerCart(java.awt.Frame parent, boolean modal, IWebCustSys customerSys, int cusID, Vector<OrderItem> itemList) {
        super(parent, modal);
        initComponents();

        cusSys = customerSys;
        customerID = cusID;
        orderList = itemList;

        addToList();
        
    }

    private void addToList(){
        DefaultListModel list = new DefaultListModel();
        for(OrderItem ordList: orderList) {
            String productName = cusSys.getProductInfo(ordList.getProID()).getName();
            list.addElement("Product: " + productName + " -- Ordered Amount: " + ordList.getOrdItermAmount());
        }
        listOrder.setModel(list);
    }

    private void remoOrder(){
        orderList.removeElementAt(listOrder.getSelectedIndex());
        addToList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listOrder = new javax.swing.JList();
        btnOrder = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Customer Cart");

        listOrder.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listOrder);

        btnOrder.setText("Confirm Order");
        btnOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOrderActionPerformed(evt);
            }
        });

        jButton1.setText("Remove Order");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnOrder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(btnOrder))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrderActionPerformed
        int choice = JOptionPane.showConfirmDialog(this,"Send your Order now?","Decision",0);
            if(choice != 0) return;
            else cusSys.sendOrder(customerID, orderList);
    }//GEN-LAST:event_btnOrderActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(listOrder.getSelectedIndex() == -1){
            JOptionPane.showMessageDialog(this,"Please select a Order Item to remove","Error",0);
            return;
        } else {
            int choice = JOptionPane.showConfirmDialog(this,"Really want to remove the selected Order Item?","Decision",0);
            if(choice != 0) return;
            else {
                remoOrder();
                JOptionPane.showMessageDialog(this,"Order Sent Successfully!");
            }}
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOrder;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listOrder;
    // End of variables declaration//GEN-END:variables

}
